<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- rm | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- rm</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux中删除文件和目录的命令： rm命令。rm是常用的命令，该命令的功能为删除一个目录中的一个或多个文件或目录，它也可以将某个目录及其下的所有文件及子目录均删除。对于链接文件，只是删除了链接，原有文件均保持不变。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rm是一个危险的命令，使用的时候要特别当心，尤其对于新手，否则整个系统就会毁在这个命令（比如在/（根目录）下执行rm * -rf）。所以，我们在执行rm之前最好先确认一下在哪个目录，到底要删除什么东西，操作时保持高度清醒的头脑。</p>
<h2 id="1．命令格式："><a href="#1．命令格式：" class="headerlink" title="1．命令格式："></a>1．命令格式：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm [选项] [文件]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能："><a href="#2．命令功能：" class="headerlink" title="2．命令功能："></a>2．命令功能：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;删除一个目录中的一个或多个文件或目录，如果没有使用- r选项，则rm不会删除目录。如果使用 rm 来删除文件，通常仍可以将该文件恢复原状。</p>
<h2 id="3．命令参数："><a href="#3．命令参数：" class="headerlink" title="3．命令参数："></a>3．命令参数：</h2><ul>
<li>-f, –force    忽略不存在的文件，从不给出提示。      </li>
<li>-i, –interactive 进行交互式删除      </li>
<li>-r, -R, –recursive   指示rm将参数中列出的全部目录和子目录均递归地删除。      </li>
<li>-v, –verbose    详细显示进行的步骤      <ul>
<li>–help     显示此帮助信息并退出      </li>
<li>–version  输出版本信息并退出      </li>
</ul>
</li>
</ul>
<h2 id="4．命令实例："><a href="#4．命令实例：" class="headerlink" title="4．命令实例："></a>4．命令实例：</h2><h3 id="实例1：删除文件file，系统会先询问是否删除。"><a href="#实例1：删除文件file，系统会先询问是否删除。" class="headerlink" title="实例1：删除文件file，系统会先询问是否删除。"></a>实例1：删除文件file，系统会先询问是否删除。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm [文件名]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 4</div><div class="line">-rw-r--r-- 1 root root 56 10-26 14:31 log.log</div><div class="line">root@localhost test1]<span class="comment"># rm log.log </span></div><div class="line">rm：是否删除 一般文件 “log.log”? y</div><div class="line">root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 0[root@localhost test1]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输入rm log.log命令后，系统会询问是否删除，输入y后就会删除文件，不想删除则数据n。</p>
<h3 id="实例2：强行删除file，系统不再提示。"><a href="#实例2：强行删除file，系统不再提示。" class="headerlink" title="实例2：强行删除file，系统不再提示。"></a>实例2：强行删除file，系统不再提示。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -f log1.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 4</div><div class="line">-rw-r--r-- 1 root root 23 10-26 14:40 log1.log</div><div class="line">[root@localhost test1]<span class="comment"># rm -f log1.log </span></div><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 0</div><div class="line">[root@localhost test1]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例3：删除任何-log文件；删除前逐一询问确认"><a href="#实例3：删除任何-log文件；删除前逐一询问确认" class="headerlink" title="实例3：删除任何.log文件；删除前逐一询问确认"></a>实例3：删除任何.log文件；删除前逐一询问确认</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -i *.<span class="built_in">log</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 8</div><div class="line">-rw-r--r-- 1 root root 11 10-26 14:45 log1.log</div><div class="line">-rw-r--r-- 1 root root 24 10-26 14:45 log2.log</div><div class="line">[root@localhost test1]<span class="comment"># rm -i *.log</span></div><div class="line">rm：是否删除 一般文件 “log1.log”? y</div><div class="line">rm：是否删除 一般文件 “log2.log”? y</div><div class="line">[root@localhost test1]<span class="comment"># ll</span></div><div class="line">总计 0</div><div class="line">[root@localhost test1]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例4：将-test1子目录及子目录中所有档案删除"><a href="#实例4：将-test1子目录及子目录中所有档案删除" class="headerlink" title="实例4：将 test1子目录及子目录中所有档案删除"></a>实例4：将 test1子目录及子目录中所有档案删除</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -r test1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 24</div><div class="line">drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxr-xr-x 2 root root 4096 10-26 14:51 test1</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:44 test2</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># rm -r test1</span></div><div class="line">rm：是否进入目录 “test1”? y</div><div class="line">rm：是否删除 一般文件 “test1/log3.log”? y</div><div class="line">rm：是否删除 目录 “test1”? y</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 20drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:44 test2</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例5：rm-rf-test2命令会将-test2-子目录及子目录中所有档案删除-并且不用一一确认"><a href="#实例5：rm-rf-test2命令会将-test2-子目录及子目录中所有档案删除-并且不用一一确认" class="headerlink" title="实例5：rm -rf test2命令会将 test2 子目录及子目录中所有档案删除,并且不用一一确认"></a>实例5：rm -rf test2命令会将 test2 子目录及子目录中所有档案删除,并且不用一一确认</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -rf  test2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># rm -rf test2</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 16</div><div class="line">drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例6：删除以-f-开头的文件"><a href="#实例6：删除以-f-开头的文件" class="headerlink" title="实例6：删除以 -f 开头的文件"></a>实例6：删除以 -f 开头的文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm -- -f</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># touch -- -f</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -- -f</span></div><div class="line">-f[root@localhost <span class="built_in">test</span>]<span class="comment"># rm -- -f</span></div><div class="line">rm：是否删除 一般空文件 “-f”? y</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -- -f</span></div><div class="line">ls: -f: 没有那个文件或目录</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div><div class="line">也可以使用下面的操作步骤:</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># touch ./-f</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls ./-f</span></div><div class="line">./-f[root@localhost <span class="built_in">test</span>]<span class="comment"># rm ./-f</span></div><div class="line">rm：是否删除 一般空文件 “./-f”? y</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例7：自定义回收站功能"><a href="#实例7：自定义回收站功能" class="headerlink" title="实例7：自定义回收站功能"></a>实例7：自定义回收站功能</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="title">myrm</span></span>()&#123; D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p <span class="variable">$D</span>; mv <span class="string">"<span class="variable">$@</span>"</span> <span class="variable">$D</span> &amp;&amp; <span class="built_in">echo</span> <span class="string">"moved to <span class="variable">$D</span> ok"</span>; &#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># myrm()&#123; D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; 	mv "$@" $D &amp;&amp; echo "moved to $D ok"; &#125;</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># alias rm='myrm'</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># touch 1.log 2.log 3.log</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 16</div><div class="line">-rw-r--r-- 1 root root    0 10-26 15:08 1.log</div><div class="line">-rw-r--r-- 1 root root    0 10-26 15:08 2.log</div><div class="line">-rw-r--r-- 1 root root    0 10-26 15:08 3.log</div><div class="line">drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># rm [123].log</span></div><div class="line">moved to /tmp/20121026150901 ok</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls /tmp/20121026150901/</span></div><div class="line">1.log  2.log  3.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面的操作过程模拟了回收站的效果，即删除文件的时候只是把文件放到一个临时目录中，这样在需要的时候还可以恢复过来。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考资料：<a href="http://codingstandards.iteye.com/blog/983531" target="_blank" rel="external">我使用过的Linux命令之rm - 删除文件或目录</a> </p>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/6. Linux 命令- rm/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
